package com.shop.controller;

import com.alibaba.fastjson.JSONObject;
import com.shop.Redis.GoodsKey;
import com.shop.pojo.Cart;
import com.shop.pojo.User;
import com.shop.result.JsonResult;
import com.shop.service.CartService;
import com.shop.service.LoginService;
import com.shop.service.impl.AliPayServiceImpl;
import com.shop.util.BaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @Autowired
    private LoginService loginService;

    @Autowired
    private AliPayServiceImpl aliPayServiceImpl;

    /*获取用户的购物车列表*/
    @RequestMapping("/getGoodsCart")
    @ResponseBody
    public JsonResult getGoodsCart(HttpServletRequest request, HttpServletResponse response) throws Exception{
        String token = request.getParameter("token");
        boolean isCookie = loginService.getUserIsTimeout(request,response,token);
        List<Cart> cartList = new ArrayList<>();

        if (isCookie){
           cartList = cartService.getGoodsCartByToken(token);
            return new JsonResult(true,cartList,null,cartList.size());
        }

        return new JsonResult(false,cartList);
    }

    /*添加商品至购物车*/
    @RequestMapping("/addGoodsToCart")
    @ResponseBody
    public JsonResult addCart(HttpServletRequest request, HttpServletResponse response) throws Exception{
        String data = request.getParameter("data");
        String token = request.getParameter("token");
        String returnMsg = "";
        boolean isCookie = loginService.getUserIsTimeout(request,response,token);

        if(isCookie){
            returnMsg = cartService.addGoodToCartByToken(data,token);
            return new JsonResult(true,returnMsg);

        }else{
            returnMsg = "当前用户登陆已失效，请重新登录后再试";
        }

        return new JsonResult(false,returnMsg);
    }

    /*删除商品*/
    @RequestMapping("/deleteGoodsToCart")
    @ResponseBody
    public JsonResult deleteGoodsCart(HttpServletRequest request, HttpServletResponse response) throws Exception{
        String data = request.getParameter("data");
        String token = request.getParameter("token");
        String returnMsg = "";
        boolean isCookie = loginService.getUserIsTimeout(request,response,token);

        if(isCookie){
            returnMsg = cartService.deleteGoodToCartByToken(data,token);
            return new JsonResult(true,returnMsg);

        }else{
            returnMsg = "当前用户登陆已失效，请重新登录后再试";
        }

        return new JsonResult(false,returnMsg);
    }

    /*清空购物车*/
    @RequestMapping("/deleteAllGoodsToCart")
    @ResponseBody
    public JsonResult deleteAllGoodsToCart(HttpServletRequest request, HttpServletResponse response) throws Exception{
        String token = request.getParameter("token");
        String returnMsg = "";
        boolean isCookie = loginService.getUserIsTimeout(request,response,token);

        if(isCookie){
            returnMsg = cartService.deleteAllGoodToCartByToken(token);
            return new JsonResult(true,returnMsg);

        }else{
            returnMsg = "当前用户登陆已失效，请重新登录后再试";
        }

        return new JsonResult(false,returnMsg);
    }

    /*提交购物车商品*/
    @RequestMapping("/commitUserCartToPay")
    @ResponseBody
    public JsonResult commitUserCartToPay(HttpServletRequest request, HttpServletResponse response) throws Exception{
        String data = cartService.commitUserCartToPay(request,response);
        return new JsonResult(true,null,data);
    }

    /*支付返回信息*/
    @RequestMapping("/payReturnMsg")
    @ResponseBody
    public JsonResult payReturnMsg(HttpServletRequest request, HttpServletResponse response) throws Exception{
        String msg = aliPayServiceImpl.alipayReturn(request);
        return new JsonResult(true,null,msg);
    }

}
